/**
 * \file TQueue.h
 * \brief Template queues
 * \author Corey Tabaka
 *
 * Copyright (C) Corey Tabaka 2007.
 * All rights reserved.
 */

/*
   Copyright 2007 Corey Tabaka

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#include <ktypes.h>
#include <Sync.h>

#ifndef __KERNEL_TQUEUE_H__
#define __KERNEL_TQUEUE_H__

template<class T, class LOCK=Mutex>
class CircularQueue {
	protected:
		
	T *buf;
	uint32 size;
	uint32 in, out; // queue index pointers
	
	LOCK lock;
	
	public:
	
	CircularQueue(uint32 size) : lock() {
		this->size = size;
		in = out = 0;
		
		buf = new T[size];
	}
	
	virtual ~CircularQueue(void) {
		delete [] buf;
	}
	
	bool isEmpty(void) {
		AutoLock<LOCK> _auto(lock);
		//lock.lock();
		
		bool e = in == out;
		
		//lock.unlock();
		return e;
	}
	
	bool isFull(void) {
		AutoLock<LOCK> _auto(lock);
		//lock.lock();
		
		bool f = (out - in) == 1 || (out == 0 && in == (size-1));
		
		//lock.unlock();
		return f;
	}
	
	bool enqueue(T data) {
		AutoLock<LOCK> _auto(lock);
		//lock.lock();
		
		uint32 temp;
		
		temp = in + 1;
		if (temp >= size) {
			temp = 0;
		}
		
		// if in reaches out, the queue is full
		if (temp == out) {
			//lock.unlock();
			return false;
		}
		
		buf[in] = data;
		in = temp;
		
		//lock.unlock();
		return true;
	}
	
	bool peek(T &rvalue) {
		AutoLock<LOCK> _auto(lock);
		//lock.lock();
		
		if (in == out) {
			//lock.unlock();
			return false;
		}
		
		rvalue = buf[out];
		
		//lock.unlock();
		return true;
	}
	
	bool dequeue(T &rvalue) {
		AutoLock<LOCK> _auto(lock);
		//lock.lock();
	
		// if out reaches in, the queue is empty
		if (out == in) {
			//lock.unlock();
			return false;
		}
		
		rvalue = buf[out];
		out++;
		
		if (out >= size) {
			out = 0;
		}
		
		//lock.unlock();
		return true;
	}
};

template<class T, class LOCK=Mutex>
class RWCircularQueue {
	protected:
		
	T *buf;
	uint32 size;
	uint32 in, out; // queue index pointers
	
	LOCK lock;
	Semaphore full;
	Semaphore empty;
	
	public:
	
	RWCircularQueue(uint32 size) : lock(), full(0), empty(size) {
		this->size = size;
		in = out = 0;
		
		buf = new T[size];
	}
	
	virtual ~RWCircularQueue(void) {
		delete [] buf;
	}
	
	bool isEmpty(void) {
		lock.lock();
		
		bool e = in == out;
		
		lock.unlock();
		return e;
	}
	
	bool isFull(void) {
		lock.lock();
		
		bool f = (out - in) == 1 || (out == 0 && in == (size-1));
		
		lock.unlock();
		return f;
	}
	
	void enqueue(T data) {
		empty.wait();
		lock.lock();
		
		uint32 temp;
		
		temp = in + 1;
		if (temp >= size) {
			temp = 0;
		}
		
		buf[in] = data;
		in = temp;
		
		lock.unlock();
		full.signal();
	}
	
	bool peek(T &rvalue) {
		lock.lock();
		
		if (in == out) {
			lock.unlock();
			return false;
		}
		
		rvalue = buf[out];
		
		lock.unlock();
		return true;
	}
	
	void dequeue(T &rvalue) {
		full.wait();
		lock.lock();
		
		rvalue = buf[out];
		out++;
		
		if (out >= size) {
			out = 0;
		}
		
		lock.unlock();
		empty.signal();
	}
};

#endif
